﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class PassengerRepository : HitchHikersGuideToTheGalaxy.Models.IPassengerRepository
    {
        GalaxyDataContext db = new GalaxyDataContext();

        #region Default Add, Delete and save actions

        /// <summary>
        /// Add one Passanger record to the database.
        /// </summary>
        /// <param name="passenger"></param>
        public void Add(Passenger passenger)
        {
            db.Passengers.InsertOnSubmit(passenger);
        }

        /// <summary>
        /// Delete one Passenger record from database.
        /// </summary>
        /// <param name="passenger"></param>
        public void Delete(Passenger passenger)
        {
            db.Passengers.DeleteOnSubmit(passenger);
        }

        /// <summary>
        /// Commit changes made to database.
        /// </summary>
        public void Save()
        {
            db.SubmitChanges();
        }

        #endregion

        #region Querys

        /// <summary>
        /// Fetch all Passenger records from database.
        /// </summary>
        /// <returns></returns>
        public System.Linq.IQueryable<Passenger> FindAllPassengers()
        {
            return db.Passengers;
        }

        /// <summary>
        /// Get one Passenger record from database.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Passenger GetPassenger(int id)
        {
            return db.Passengers.SingleOrDefault(d => d.Id == id);
        }

        /// <summary>
        /// Get on Passenger record from TourId
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Passenger GetPassengersByTourId(int id)
        {
            return db.Passengers.SingleOrDefault(d => d.TourId == id);
        }

        /// <summary>
        /// Change passenger acceptance status
        /// </summary>
        /// <param name="tourId"></param>
        /// <param name="passengerSSN"></param>
        /// <param name="accepted"></param>
        /// <param name="repo"></param>
        public void ChangePassengerAcceptedStatus(int tourId, string passengerSSN, char accepted, IPassengerRepository repo)
        {
            var pass = (from v in db.Passengers
                        where v.TourId == tourId && v.UserInfoSSN == passengerSSN
                        select v).Single();
            pass.Accepted = accepted;
            repo.Save();
        }

        #endregion
    }
}
